package drag;

import java.rmi.RemoteException;
import javafx.event.EventHandler;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import model.Quadro;
import model.TAGS;
import rmi.RMIServer;

/**
 * Classe que controes os imoveis nos terrenos
 *
 * @author Adriano
 */
public class Construtora {

    private Quadro quadro;
    private DragOver dragOver;
    private DragEntered dragEntered;
    private DragExited dragExited;
    private DragDropped dragDropped;
    private DragDone dragDone;

    public Construtora(Quadro quadro) {
        this.quadro = quadro;
        this.dragOver = new DragOver();
        this.dragEntered = new DragEntered();
        this.dragExited = new DragExited();
        this.dragDropped = new DragDropped();
        this.dragDone = new DragDone();
    }

    /**
     * Classe que implementa os metodos do Dragevent e é chamado quando há uma
     * inteção de construção
     */
    public class DragOver implements EventHandler<DragEvent> {

        @Override
        public void handle(DragEvent event) {
            String type = event.getDragboard().getString();
            if (event.getGestureSource() != quadro && event.getDragboard().hasImage() && TAGS.POSITION == quadro.getCampoPosition() && TAGS.NAME.equals(quadro.getDono())) {
                if (type.equals("casa") && quadro.getCasa() < 4) {
                    event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                } else if (type.equals("hotel") && quadro.getCasa() == 4) {
                    event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                }
            }
            event.consume();
        }
    }

    /**
     * Classe que implementa os metodos do Dragevent e é chamado para os campos
     * que permitem essa operação
     */
    public class DragEntered implements EventHandler<DragEvent> {

        @Override
        public void handle(DragEvent event) {
            String type = event.getDragboard().getString();
            if (event.getGestureSource() != quadro && event.getDragboard().hasImage()
                    && TAGS.POSITION == quadro.getCampoPosition()
                    && TAGS.NAME.equals(quadro.getDono())) {
                if (type.equals("casa") && quadro.getCasa() < 4) {
                    quadro.setStyle("-fx-background-color:#ff6666;");
                } else if (type.equals("hotel") && quadro.getCasa() == 4) {
                    quadro.setStyle("-fx-background-color:#ff6666;");
                }
            }
            event.consume();
        }
    }

    /**
     * Classe que implementa os metodos do Dragevent e ao termio da operação
     */
    public class DragExited implements EventHandler<DragEvent> {

        @Override
        public void handle(DragEvent event) {
            if (TAGS.POSITION == quadro.getCampoPosition()) {
                quadro.setStyle("-fx-background-color:#ffffff;");
                quadro.setId("casa");
            }
        }
    }

    /**
     * Classe que implementa os metodos do Dragevent e é chamado quando a
     * operação é permitida
     */
    public class DragDropped implements EventHandler<DragEvent> {

        @Override
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();

            boolean success = false;
            String type = event.getDragboard().getString();
            if (db.hasImage() && TAGS.POSITION == quadro.getCampoPosition() && TAGS.NAME.equals(quadro.getDono())) {

                if (type.equals("casa") && quadro.getCasa() < 4) {
                    try {
                        RMIServer.getInstacia().getJogadas().colocarCasa(TAGS.NAME, TAGS.POSITION);
                    } catch (RemoteException ex) {
                        ex.printStackTrace();
                    }
                    success = true;
                } else if (type.equals("hotel") && quadro.getCasa() == 4) {
                    try {
                        RMIServer.getInstacia().getJogadas().colocarCasa(TAGS.NAME, TAGS.POSITION);
                    } catch (RemoteException ex) {
                        ex.printStackTrace();
                    }
                }
                quadro.setStyle("-fx-background-color:#ff6666;");
            }
            event.setDropCompleted(success);
            quadro.setStyle("-fx-background-color:#ffffff;");
            event.consume();
        }
    }

    public class DragDone implements EventHandler<DragEvent> {

        @Override
        public void handle(DragEvent event) {
            quadro.setStyle("-fx-background-color:#ffffff;");
        }
    }

    public DragOver getDragOver() {
        return dragOver;
    }

    public DragEntered getDragEntered() {
        return dragEntered;
    }

    public DragExited getDragExited() {
        return dragExited;
    }

    public DragDropped getDragDropped() {
        return dragDropped;
    }

    public DragDone getDragDone() {
        return dragDone;
    }
}
